﻿using JobColler.builder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JobColler.util;
using System.Text.RegularExpressions;

namespace JobColler.engine
{
    class SearchEngine
    {
        /// <summary>
        /// 关键词搜索
        /// </summary>
        /// <param name="config">搜索配置</param>
        /// <param name="pager">页码</param>
        /// <returns></returns>
        public static List<CompanyInfoBuiler> search(SearchConfigBuilder config, int pager)
        {
            String url = config.getSearchUrl(pager);
            String html = HttpUtils.request(url, "get", "", "gbk");
            List<String> jobbers = textCutCenters(html, "<div class=\"el\">", "</div>");
            if (jobbers == null || jobbers.Count == 0)
            {
                return null;
            }
            List<CompanyInfoBuiler> results = new List<CompanyInfoBuiler>();
            foreach (String jobber in jobbers)
            {
                try
                {
                    String titleHtml = textCutCenter(jobber, " <span class=\"t2\">", "</span>");
                    String title = textCutCenter(titleHtml, ">", "<");
                    String compUrl = textCutCenter(titleHtml, "href=\"", "\"");
                    CompanyInfoBuiler result = new CompanyInfoBuiler();
                    result.CompName = title;
                    result.Url = compUrl;
                    results.Add(result);
                }
                catch { }

            }
            return results;
        }
        /// <summary>
        /// 加载公司详情
        /// </summary>
        /// <param name="companyInfo">公司详情</param>
        /// <returns></returns>
        public static CompanyInfoBuiler searchCompanyInfo(CompanyInfoBuiler companyInfo)
        {
            String html = HttpUtils.request(companyInfo.Url, "get", "", "gbk");
            String coTypeHtml = textCutCenter(html, "<a href=\"https://company.51job.com/", "/a>");
            String coType = textCutCenter(coTypeHtml, ">", "<");
            companyInfo.CompRemark = coType;

            String remarkHtml = textCutCenter(html, "<div class=\"con_txt\">", "</div>");
            String siteHtml = textCutCenter(html, "<p class=\"fp tmsg\">", "</p>");
            String domain = exportDomain(remarkHtml + siteHtml);
            companyInfo.CompSite = domain;


            return companyInfo;
        }
        /// <summary>
        /// 域名后缀
        /// </summary>
        private static List<String> suffixs = new List<string>();
        /// <summary>
        /// 邮箱后缀
        /// </summary>
        private static List<String> mailSuffixs = new List<string>();

        static SearchEngine()
        {
            String suffixContext = JobColler.Properties.Resources.domain_suffix;
            String[] suffixArray = suffixContext.Split("\r\n".ToArray());
            suffixs = suffixArray.ToList<String>();
            String[] mails = "@163.com;@126.com;@163.net;@188.com;@yeah.net;@gmail.com;@googlemail.com;@hotmail.com;@yahoo.com;@yahoo.com.cn;@sina.com;@sohu.com;@tom.com;@21cn.com;@qq.com;@263.net;@189.cn;@139.com;@eyou.com;@sogou.com".Split(";".ToCharArray());
            mailSuffixs = mails.ToList<String>();
        }


        private static String exportDomain(String context)
        {
            try
            {
                Regex regex = new Regex("(http\\://|https\\://)?([a-zA-Z0-9\\-]{1,20}\\.)?[@a-zA-Z0-9\\-]{1,20}\\.[a-zA-Z0-9\\.]{1,10}", RegexOptions.IgnoreCase);
                MatchCollection result = regex.Matches(context);
                HashSet<String> urls = new HashSet<string>();
                foreach (Match m in result)
                {
                    if (String.IsNullOrEmpty(m.Value))
                    {
                        continue;
                    }
                    String url = m.Value.Trim();
                    String suffix = getSuffix(url);
                    if (suffixs.Contains(suffix))
                    {
                      foreach (String mail in mailSuffixs)
                        {
                            if (url.Contains(mail))
                            {
                                url = null;
                                break;
                            }
                        }
                        if (String.IsNullOrEmpty(url)) {
                            continue;
                        }
                        if (url.Contains("@"))
                        {
                            int index = url.IndexOf("@");
                            url = url.Substring(index + 1, url.Length - index - 1);
                        }
                        if (!url.StartsWith("http://") && !url.StartsWith("https://"))
                        {
                            url = "http://" + url;
                        }
                        urls.Add(url);
                    }
                }
                if (urls.Count == 0)
                {
                    return "";
                }
                StringBuilder domain = new StringBuilder();
                foreach (String url in urls)
                {
                    domain.Append(url).Append(" ");
                }
                return domain.ToString();
            }
            catch
            {
                return null;
            }
        }


        private static String getSuffix(String url)
        {
            int index = url.LastIndexOf(".");
            if (index < 0)
            {
                return "";
            }
            return url.Substring(index, url.Length - index);
        }
        private static String textCutCenter(String allTxt, String firstTxt, String lastTxt)
        {
            try
            {
                String tmp = "";
                int n1 = allTxt.IndexOf(firstTxt);
                if (n1 == -1)
                {
                    return "";
                }
                tmp = allTxt.Substring(n1 + firstTxt.Length, allTxt.Length - (n1 + firstTxt.Length));
                int n2 = tmp.IndexOf(lastTxt);
                if (n2 == -1)
                {
                    return "";
                }
                tmp = tmp.Substring(0, n2);
                return tmp;
            }
            catch
            {
                return "";
            }
        }

        private static List<String> textCutCenters(String allTxt, String firstTxt, String lastTxt)
        {
            try
            {
                List<String> results = new List<string>();
                while (allTxt.Contains(firstTxt))
                {
                    int n = allTxt.IndexOf(firstTxt);
                    allTxt = allTxt.Substring(n + firstTxt.Length, allTxt.Length - (n + firstTxt.Length));
                    n = allTxt.IndexOf(lastTxt);
                    if (n == -1)
                    {
                        return results;
                    }
                    String result = allTxt.Substring(0, n);
                    results.Add(result);
                    allTxt = allTxt.Substring(n + firstTxt.Length, allTxt.Length - (n + firstTxt.Length));
                }
                return results;
            }
            catch
            {
                return null;
            }
        }
    }



}
